<template>
  <div class="MarketActivity-container">
    <el-form
      :inline="true"
      ref="activityQueryFormRef"
      :model="activityQuery"
      class="demo-form-inline"
    >
      <el-form-item label="负责人">
        <el-select v-model="activityQuery.ownerId" placeholder="请选择负责人名称" clearable>
          <el-option
            v-for="owner in ownerList"
            :key="owner.id"
            :label="owner.name"
            :value="owner.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动名称">
        <el-input v-model="activityQuery.name" placeholder="请输入活动名称" clearable />
      </el-form-item>
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="activityTime"
          type="datetimerange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          date-format="YYYY/MM/DD ddd"
          time-format="A hh:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="活动预算">
        <el-input v-model="activityQuery.cost" placeholder="请输入活动预算" clearable />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="activityQuery.createTime"
          type="datetime"
          placeholder="请选择活动创建时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜 索</el-button>
        <el-button type="primary" @click="onReset" plain>重 置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="addActivity">录入市场活动</el-button>
    <el-button type="danger" @click="batchDel">批量删除</el-button>
    <el-table
      :data="activityList"
      style="width: 100%; margin-top: 12px"
      @selection-change="selectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="120" />
      <el-table-column prop="ownerDO.name" label="负责人" width="120" show-overflow-tooltip />
      <el-table-column prop="name" label="活动名称" width="120" show-overflow-tooltip />
      <el-table-column prop="startTime" label="开始时间" show-overflow-tooltip />
      <el-table-column prop="endTime" label="结束时间" show-overflow-tooltip />
      <el-table-column prop="cost" label="活动预算" show-overflow-tooltip />
      <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip />
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleView(scope.row.id)">
            详情
          </el-button>
          <el-button type="primary" size="small" @click="handleEdit(scope.row.id)">
            编辑
          </el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="total"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="total"
      class="pagination"
      @change="pageChange"
      @prev-click="toPage"
      @next-click="toPage"
    />
  </div>
</template>

<script setup lang="ts" name="MarketActivity">
// type Props = {}
// defineProps<Props>()
// const emit = defineEmits([])
// const router = useRouter()
// const route = useRoute()
import httpRequest from '@/http/httpRequest'

import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'
const router = useRouter()
const activityQueryFormRef = ref<FormInstance>()
interface IActivity {
  ownerId: number | null
  name: string | null
  startTime: string | null
  endTime: string | null
  cost: number | null
  createTime: string | null
  ownerDO: TUser | null
}
interface TUser {
  id: number | null
  name: string | null
}
let activityQuery = reactive<IActivity>({
  ownerId: null,
  name: null,
  startTime: null,
  endTime: null,
  cost: null,
  createTime: null,
  ownerDO: null,
})
let activityList = ref<IActivity[]>([])
let activityTime = ref('')

const onReset = () => {
  activityQueryFormRef.value?.resetFields()
  Object.assign(activityQuery, {
    ownerId: null,
    name: null,
    startTime: null,
    endTime: null,
    cost: null,
    createTime: null,
    ownerDO: null,
  })
  activityTime.value = ''
  getData(1)
}
const onSearch = () => {
  getData(1)
}
const addActivity = () => {
  router.push({
    path: '/home/market-activity-add',
  })
}
const batchDel = () => {}
const selectionChange = (newSelection: any[]) => {
  console.log('selectionChange', newSelection)
}
const handleView = (id: number) => {
  router.push({
    path: '/home/market-activity-detail/' + id,
  })
}
const handleEdit = (id: number) => {
  router.push({
    path: '/home/market-activity-edit/' + id,
  })
}
const handleDelete = (id: number) => {}

onMounted(() => {
  getData(1)
  getOwnerList()
})

const getData = (currentPage: number, pageSize: number = 10) => {
  if (activityTime.value && activityTime.value.length == 2) {
    activityQuery.startTime = activityTime.value[0]
    activityQuery.endTime = activityTime.value[1]
  }
  httpRequest
    .doGet('/api/activitys', {
      currentPage: currentPage,
      pageSize: pageSize,
      ...activityQuery,
    })
    .then((res) => {
      if (res.data.code == 200) {
        activityList.value = res.data.data.list || []
        total.value = res.data.data.total
      }
    })
    .catch((error) => {})
}

let total = ref(0)
let pageSize = ref(10)
let pageChange = (currentPage: number, pageSize: number) => {
  getData(currentPage, pageSize)
}

let toPage = (currentPage: number) => {
  getData(currentPage, pageSize.value)
}

interface IOwnerList {
  id: number | null
  name: string | null
}
let ownerList = ref<IOwnerList[]>([])
const getOwnerList = () => {
  httpRequest
    .doGet('/api/ownerList', {})
    .then((res) => {
      if (res.data.code == 200) {
        ownerList.value = res.data.data || []
      }
    })
    .catch((error) => {})
}
</script>

<style scoped lang="scss">
.MarketActivity-container {
  min-height: 100%;

  .demo-form-inline .el-input {
    --el-input-width: 220px;
  }

  .demo-form-inline .el-select {
    --el-select-width: 220px;
  }
}
</style>
